package com.ysx.server.controller.admin;

import com.github.pagehelper.Page;
import com.ysx.server.pojo.dto.GoodsDTO;
import com.ysx.server.pojo.dto.GoodsPageQueryDTO;
import com.ysx.server.pojo.entity.Goods;
import com.ysx.server.pojo.vo.PageQueryVO;
import com.ysx.server.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/goods")
@Slf4j
public class GoodsManageController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询
     * @param goodsPageQueryDTO
     * @return
     */
    @GetMapping("/goods")
    public PageQueryVO<Goods> goodsPageQuery(GoodsPageQueryDTO goodsPageQueryDTO){
        log.info("查询商品：{}",goodsPageQueryDTO);
        if(goodsPageQueryDTO==null){
            return null;
        }
        Page<Goods> page = goodsService.page(goodsPageQueryDTO);
        long total = page.getTotal();
        List<Goods> result = page.getResult();
        return new PageQueryVO<Goods>(total,result);
    }

    /**
     * 更新商品信息
     * @param goodsDTO
     * @return
     */
    @PostMapping("/update")
    @CacheEvict(cacheNames = {"goodsCache_new","goodsCache_all","goodsCache"},allEntries=true)
    public boolean updateUser(@RequestBody GoodsDTO goodsDTO){
        if(goodsDTO==null){
            return false;
        }
        return goodsService.update(goodsDTO);
    }

    /**
     * 删除商品
     * @param id 用户id
     * @return
     */
    @GetMapping("/delGoods/{id}")
    @CacheEvict(cacheNames = {"goodsCache_new","goodsCache_all","goodsCache"},allEntries=true)
    public boolean delUser(@PathVariable Long id){
        log.info("删除用户id：{}",id);
        if(id==null||id<0) {
            return false;
        }
        return goodsService.delete(id);
    }

}
